<template>
  <el-dialog title="代金券批量新增" :close-on-click-modal="false" :visible.sync="visible" fullscreen center>
		<el-button class="miui-dialog-close-btn" @click="visible = false">关闭页面</el-button>
    <el-form class="verify-form" :model="dataForm" :rules="dataRule" ref="dataForm" label-width="100px">
      <el-row>
        <el-col :span="12">
          <el-form-item label="代金券号">
            <el-row>
              <el-col :span="12">
                <el-form-item label="固定号段" prop="vouchersNumBefore" label-width="70px">
                  <el-input v-model="dataForm.vouchersNumBefore"></el-input>
                </el-form-item>
              </el-col>
              <el-col :span="12">
                <el-form-item label="动态号段" prop="vouchersNumAfter" label-width="70px">
                  <el-input v-model="dataForm.vouchersNumAfter"></el-input>
                </el-form-item>
              </el-col>
            </el-row>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="新增数量">
            <el-input v-model="dataForm.number"></el-input>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="12">
          <el-form-item label="代金券金额" prop="money">
            <el-input v-model="dataForm.money" placeholder="代金券金额"></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="使用限制" prop="restrictions">
            <el-input v-model="dataForm.restrictions" placeholder="使用限制金额(多少元以上可使用)"></el-input>
          </el-form-item>
        </el-col>

      </el-row>
      <el-row>
        <el-col :span="12">
          <el-form-item label="有效期" required>
            <el-col :span="11">
              <el-form-item prop="validityStart">
                <el-date-picker type="date" placeholder="选择日期" v-model="dataForm.validityStart" value-format="yyyy-MM-dd" style="width: 100%;"></el-date-picker>
              </el-form-item>
            </el-col>
            <el-col class="line" :span="2" style="text-align: center;">至</el-col>
            <el-col :span="11">
              <el-form-item prop="validityEnd">
                <el-date-picker type="date" placeholder="选择日期" v-model="dataForm.validityEnd" value-format="yyyy-MM-dd" style="width: 100%;"></el-date-picker>
              </el-form-item>
            </el-col>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="消费类型" prop="typeIds">
            <el-checkbox-group v-model="dataForm.typeIds" size="mini">
              <el-checkbox :label="1" name="typeIds">框镜</el-checkbox>
              <el-checkbox :label="2" name="typeIds">隐形</el-checkbox>
              <el-checkbox :label="3" name="typeIds">辅料</el-checkbox>
              <el-checkbox :label="4" name="typeIds">挂号</el-checkbox>
              <el-checkbox :label="5" name="typeIds">维修费</el-checkbox>
            </el-checkbox-group>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-form-item label="备注">
          <el-input type="textarea" v-model="dataForm.memo"></el-input>
        </el-form-item>
      </el-row>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button type="primary" @click="dataFormSubmit" :loading="ischeck" v-prevent-re-click>保 存</el-button>
      <el-button @click="visible = false">清 空</el-button>
    </span>
  </el-dialog>
</template>

<script>
    import {getVouchers, addBatchVouchers, updateVouchers} from '@/api/eims/vouchers'

    export default {
    data () {
      return {
        ischeck: false,
        visible: false,
        dataForm: {
          id: 0,
          vouchersNum: '',
          vouchersNumBefore: '',
          vouchersNumAfter: '',
          number: '',
          money: '',
          restrictions: '',
          validityStart: '',
          validityEnd: '',
          typeIds: []
        },
        dataRule: {
          vouchersNumBefore: [{ required: true, message: '代金券固定号段不能为空', trigger: 'blur' }],
          vouchersNumAfter: [{ required: true, message: '代金券动态字段不能为空', trigger: 'blur' }],
          number: [{ required: true, message: '新增数量不能为空', trigger: 'blur' }],
          money: [{ required: true, message: '代金券金额不能为空', trigger: 'blur' }],
          restrictions: [{ required: true, message: '使用限制金额不能为空', trigger: 'blur' }],
          validityStart: [{ required: true, message: '有效期开始不能为空', trigger: 'blur' }],
          validityEnd: [{ required: true, message: '有效期结束不能为空', trigger: 'blur' }],
          typeIds: [{ required: true, message: '请至少选择一个活动性质', trigger: 'blur' }],
        }
      }
    },
    methods: {
      init (data) {
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.ischeck = true
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            addBatchVouchers(this.dataForm).then(response => {
							if(response.data.code == '0'){
							  this.$message.success('添加成功')
							  this.visible = false
							  this.$emit('refreshDataList')
							}else{
							  this.$message.error(response.data.msg)
							}
              this.ischeck = false
            }).catch(() => {
              this.ischeck = false
            })
          }else{
            this.ischeck = false
          }
        })
      }
    }
  }
</script>
